package com.sagereal.wtc.factorymode.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sagereal.wtc.factorymode.Activity.Item;
import com.sagereal.wtc.factorymode.R;

import java.util.List;

public class TestReportAdapter extends ArrayAdapter<Item> {
    private int resourceId;

    public TestReportAdapter(@NonNull Context context, int resource, @NonNull List<Item> objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Item item = getItem(position);
        View view;
        ViewHolder viewHolder;

        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.name = view.findViewById(R.id.item_name);
            viewHolder.status = view.findViewById(R.id.item_status);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        // 设置要显示的文字
        viewHolder.name.setText(item.getName());
        viewHolder.status.setText(item.getStatus());
        // 根据状态设置背景颜色
        switch (item.getStatus()) {
            case "pass":
                view.setBackgroundColor(Color.GREEN);
                break;
            case "fail":
                view.setBackgroundColor(Color.RED);
                break;
            default:
                view.setBackgroundColor(Color.GRAY);
                break;
        }
        return view;
    }
    static class ViewHolder {
        TextView name;
        TextView status;
    }
}
